/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Util;

import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import th.co.tac.esv.pre.web.util.conf.Constant;
import th.co.tac.esv.pre.web.util.conf.LogUtil;

/**
 *
 * @author Peat
 */
public class Log {

    private Logger normLogger;
    private Logger transLogger;

    private long startTime = 0;

    public enum ACTIVITY {
        PROC (Constant.PROC),
        SENT (Constant.SENT),
        RECV (Constant.RECV);

        private String s;
        ACTIVITY(String s ) {
            this.s = s;
        }

        @Override
        public String toString() {
            return s;
        }
    };

    public Log(Logger normLogger, Logger transLogger) {
        this.normLogger = normLogger;
        this.transLogger = transLogger;
    }

    public void debug(HttpServletRequest request, ACTIVITY activity, String message, Exception e) {
        log(Priority.DEBUG_INT, request, activity, message, e);
    }

    public void info(HttpServletRequest request, ACTIVITY activity, String message) {
        log(Priority.INFO_INT, request, activity, message, null);
    }
    
    public void error(HttpServletRequest request, ACTIVITY activity, String message, Exception e) {
        log(Priority.ERROR_INT, request, activity, message, e);
    }

    public void fatal(HttpServletRequest request, ACTIVITY activity, String message, Exception e) {
        log(Priority.ERROR_INT, request, activity, message, e);
    }

    public void warn(HttpServletRequest request, ACTIVITY activity, String message, Exception e) {
        log(Priority.ERROR_INT, request, activity, message, e);
    }

    public void stampIn() {
        startTime = System.currentTimeMillis();
    }

    public void trans_success(HttpServletRequest request, String majorServiceId, String minorServiceId, String message) {
        trans(Constant.STATUS_CODE_SUCCESS, request, majorServiceId, minorServiceId, message);
    }

    public void trans_bizFail(HttpServletRequest request, String majorServiceId, String minorServiceId, String message) {
        trans(Constant.STATUS_CODE_BIZ_FAIL, request, majorServiceId, minorServiceId, message);
    }

    public void trans_techFail(HttpServletRequest request, String majorServiceId, String minorServiceId, String message) {
        trans(Constant.STATUS_CODE_TECH_FAIL, request, majorServiceId, minorServiceId, message);
    }

    public void trans_unknown(HttpServletRequest request, String majorServiceId, String minorServiceId, String message) {
        trans(Constant.STATUS_CODE_UNKNOWN, request, majorServiceId, minorServiceId, message);
    }

    private void trans(String statusCode, HttpServletRequest request, String majorServiceId, String minorServiceId, String message) {
        if (transLogger!=null)
            LogUtil.logTrns(
                    transLogger,                            // module depends
                    request.getSession().getId(),
                    majorServiceId,                         // module depends
                    minorServiceId,                            // module depends
                    request.getRemoteAddr(),
                    "" + (System.currentTimeMillis() - startTime),
                    (String) request.getSession().getAttribute("username"),
                    "", "",                             // N/A on menu + func ID.
                    "temp" + request.getSession().getId(),
                    statusCode,                         // function response depends
                    "",                                 // no error code
                    message, "", "");
    }

    private void log(int level, HttpServletRequest request, ACTIVITY activity, String message, Exception e) {
        if (normLogger!=null)
            LogUtil.log(request.getSession().getId(),
                (String) request.getSession().getAttribute("username"),
                normLogger, level,
                Constant.SERVER_NAME_SSO, activity.toString(),
                message, e);
    }

}
